//
//  XMUserDetailTagsTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/2/20.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMUserDetailTagsTableViewCell.h"

@implementation XMUserDetailTagsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setupCellWithUser:(XMUserModel *)user {
    if (user.tags.count == 0) { return; }
    CGFloat leftMarginLabel = 20;
    CGFloat topMarginLabel = 0;
    CGFloat horizontalSpace = 5;
    CGFloat verticalSpace = 5;
    CGFloat labelHeight = 20;
    CGFloat windthMax = kScreenWidth - leftMarginLabel * 2;
    CGFloat labelMinX = leftMarginLabel;
    CGFloat labelMimY = topMarginLabel;
    for (int i = 0; i < user.tags.count; i++) {
        NSString *tagStr = user.tags[i];
        UIButton *labelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [labelButton setBackgroundColor:kHEXA(@"#8C8F9D", .1)];
        [labelButton setTitleColor:k8C8F9D forState:UIControlStateNormal];
        labelButton.titleLabel.font = kUIFont(12);
        labelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        labelButton.tag = i;
        CGFloat labelWidth = [tagStr widthForFont:kUIFont(12)] + 24;
        labelWidth = labelWidth < 47 ? 47 : labelWidth;
        [labelButton setTitle:[NSString stringWithFormat:@"%@", tagStr] forState:UIControlStateNormal];
        labelButton.frame = CGRectMake(labelMinX, labelMimY, labelWidth, labelHeight);
        if (labelMinX + labelWidth + horizontalSpace > windthMax) {
            labelMinX = leftMarginLabel;
            labelMimY = labelMimY + labelButton.frame.size.height + verticalSpace;
            labelButton.frame = CGRectMake(labelMinX, labelMimY, labelWidth, labelHeight);
        }
        labelMinX = labelButton.frame.size.width + labelButton.frame.origin.x + horizontalSpace;
        kCornerRadius(labelButton, 10, 0, UIColor.clearColor);
        [self.contentView addSubview:labelButton];
    }
}

@end
